﻿namespace Grove.Ui {
	using System;
	using System.IO;
	using System.Windows.Media;
	using System.Windows.Media.Imaging;

	public static class MediaLibrary {
		private const string Images = @"images\";
		private const string Cards = @"cards\";
		private const string Decks = @"decks\";
		private const string SetImages = @"images\sets\";

#if DEBUG
		private static readonly string BasePath = Path.GetFullPath(@"..\..\..\..\media\");
#else 
    private static readonly string BasePath = Path.GetFullPath(@".\media");
#endif

		public static string ImagesFolder { get { return Path.Combine(BasePath, Images); } }
		public static string DecksFolder { get { return Path.Combine(BasePath, Decks); } }
		public static string SetImagesFolder { get { return Path.Combine(BasePath, SetImages); } }


		public static ImageSource GetDefaultCardImage() {
			return GetImage("missing-card-image.jpg");
		}

		public static ImageSource GetCardImage(string name) {
			return File.Exists(GetImagePath(Cards, name))
			  ? GetImage(name, Cards)
			  : GetDefaultCardImage();
		}

		public static bool CardImageExists(string name, string set) {
			var cardPath = Path.Combine(Cards, set);
			//System.Diagnostics.Debug.WriteLine("CardImageExists: " + GetImagePath(cardPath, name));
			return File.Exists(GetImagePath(cardPath, name));
		}

		public static ImageSource GetCardImage(string name, string set) {
			string cardPath = Path.Combine(Cards, set);
			return File.Exists(GetImagePath(cardPath, name)) ? GetImage(name, cardPath) : GetDefaultCardImage();
		}

		public static ImageSource GetCardSetImage(string name, string folder = null) {
			folder = folder ?? SetImages;
			var path = GetImagePath(folder, name);
			if (File.Exists(path) == false) {
				return null;
			}

			var uri = new Uri(path);
			return new BitmapImage(uri);
		}

		public static ImageSource GetDefaultCardSetImage() {
			return GetCardSetImage("none.png", SetImages);
		}

		public static ImageSource GetImage(string name, string folder = null) {
			folder = folder ?? Images;
			var path = GetImagePath(folder, name);
			var uri = new Uri(path);
			return new BitmapImage(uri);
		}

		public static ImageSource GetImageWithPath(string path) {
			var uri = new Uri(path);
			return new BitmapImage(uri);
		}

		private static string GetImagePath(string folder, string name) {
			return Path.Combine(BasePath, folder, name);
		}

		public static Stream GetDeck(string name) {
			var path = Path.Combine(BasePath, Decks, name + ".dec");
			return File.OpenRead(path);
		}
	}
}